(function () {


    var geojsonObject = {
        'type': 'FeatureCollection',
        'crs': {
            'type': 'name',
            'properties': {
                'name': 'EPSG:3857'
            }
        },
        'features': [{
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [0, 0]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'LineString',
                'coordinates': [[4e6, -2e6], [8e6, 2e6]]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'LineString',
                'coordinates': [[4e6, 2e6], [8e6, -2e6]]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'Polygon',
                'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'MultiLineString',
                'coordinates': [
                    [[-1e6, -7.5e5], [-1e6, 7.5e5]],
                    [[1e6, -7.5e5], [1e6, 7.5e5]],
                    [[-7.5e5, -1e6], [7.5e5, -1e6]],
                    [[-7.5e5, 1e6], [7.5e5, 1e6]]
                ]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'MultiPolygon',
                'coordinates': [
                    [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6], [-3e6, 6e6]]],
                    [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6], [0, 6e6]]],
                    [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6], [3e6, 6e6]]]
                ]
            }
        }, {
            'type': 'Feature',
            'geometry': {
                'type': 'GeometryCollection',
                'geometries': [{
                    'type': 'LineString',
                    'coordinates': [[-5e6, -5e6], [0, -5e6]]
                }, {
                    'type': 'Point',
                    'coordinates': [4e6, -5e6]
                }, {
                    'type': 'Polygon',
                    'coordinates': [[[1e6, -6e6], [2e6, -4e6], [3e6, -6e6]]]
                }]
            }
        }]
    };

    var map, converLayer;
    function initMap() {
        var baselayer = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}'
            })
        });

        map = new ol.Map({
            target: 'map',
            layers: [baselayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [112, 36],
                zoom: 6
            })
        });

        var mystyle = new ol.style.Style({
            fill: new ol.style.Fill({
                color: "rgba(72,61,139, 0.4)",
            }),
            stroke: new ol.style.Stroke({
                color: "#1fbd52",
                width: 2
            })
        });
        // converLayer = new ol.layer.Vector({
        //     source: new ol.source.Vector({
        //         url: './data/shanxi_Line.json',
        //         format: new ol.format.GeoJSON()
        //     }),
        //     style: mystyle
        // });
        converLayer = new ol.layer.Vector();
        map.addLayer(converLayer);

    }

    //todo
    //添加遮罩
    function addconver(data) {
        $.getJSON(data, function (data) {
            var fts = (new ol.format.GeoJSON()).readFeatures(data);
            // var fts = (new ol.format.GeoJSON()).readFeatures(geojsonObject);
            var ft = fts[0];
            if (ft) {
                var converGeom = erase(ft.getGeometry());
            }

            var convertFt = new ol.Feature({
                geometry: converGeom
            })
            converLayer.getSource().addFeature(convertFt);
        })
    }

    // 擦除操作,生产遮罩范围
    function erase(geom) {
        var extent = [-180, -90, 180, 90];
        var polygonRing = ol.geom.Polygon.fromExtent(extent);
        var coords = geom.getCoordinates();
        coords.forEach(coord => {
            var linearRing = new ol.geom.LinearRing(coord[0]);
            polygonRing.appendLinearRing(linearRing);
        })
        return polygonRing;
    }

    initMap();
    var dataURL = './data/shanxi_Line.json'
    addconver(dataURL);
})();
